package com.zx.flow.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.zx.flow.api.execute.common.ResponseStatusAnno;
import com.zx.flow.api.execute.model.FlowContext;
import com.zx.flow.api.execute.model.FlowNode;
import com.zx.flow.api.execute.request.FlowParamAction;
import com.zx.flow.api.execute.request.FlowParamCancel;
import com.zx.flow.api.execute.request.FlowParamCommunicate;
import com.zx.flow.api.execute.request.FlowParamCommunicateAnswer;
import com.zx.flow.api.execute.request.FlowParamCreate;
import com.zx.flow.api.execute.request.FlowParamSave;
import com.zx.flow.api.execute.request.FlowParamSkip;
import com.zx.flow.api.execute.request.FlowParamStart;
import com.zx.flow.api.execute.request.FlowParamTransfer;
import com.zx.flow.api.execute.request.query.QueryFlowParam;
import com.zx.flow.api.execute.request.query.QueryNodeParam;
import com.zx.flow.api.execute.request.query.QueryNodesParam;
import com.zx.flow.api.execute.response.Response;
import com.zx.flow.api.sdk.IFlowSdk;
import com.zx.flow.core.service.FlowService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.annotations.tags.Tag;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/flow/v1",
        consumes = {MediaType.APPLICATION_JSON_UTF8_VALUE},
        produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
@Tag(name = "流程-操作")
@ResponseStatusAnno
@SecurityScheme(
        name = "JWT-test",                   // 认证方案名称
        type = SecuritySchemeType.HTTP,      // 认证类型，当前为http认证
        description = "这是一个认证的描述详细",  // 描述信息
        in = SecuritySchemeIn.HEADER,        // 代表在http请求头部
        scheme = "bearer",                   // 认证方案，如：Authorization: bearer token信息
        bearerFormat = "JWT")                // 表示使用 JWT 格式作为 Bearer Token 的格式
public class FlowV1Controller implements IFlowSdk {

    @Autowired
    private FlowService flowService;

    @Override
    @PostMapping("/get/flow")
    @Operation(summary = "流程-查询")
    @ApiOperationSupport(order = 1)// 使用 knife4j 增强，进行方法排序
    public Response<FlowContext> getFlow(@RequestBody @Validated QueryFlowParam param) {
        return this.flowService.getFlow(param.getFlowId());
    }

    @Override
    @PostMapping("/get/node")
    @Operation(summary = "流程-查询-单个节点")
    @ApiOperationSupport(order = 2)
    public Response<FlowNode> getNode(@RequestBody @Validated QueryNodeParam param) {
        return this.flowService.getNode(param.getFlowId(), param.getNodeId());
    }

    @Override
    @PostMapping("/get/nodes")
    @Operation(summary = "流程-查询-所有节点")
    @ApiOperationSupport(order = 3)
    public Response<List<FlowNode>> getNodes(@RequestBody @Validated QueryNodesParam param) {
        return this.flowService.getNodes(param.getFlowId());
    }

    @Override
    @PostMapping("/create")
    @Operation(summary = "流程-创建")
    @ApiOperationSupport(order = 4)
    public Response<FlowContext> create(@RequestBody FlowParamCreate paramCreate) {
        return this.flowService.execute(paramCreate);
    }

    @Override
    @PostMapping("/save")
    @Operation(summary = "流程-保存")
    @ApiOperationSupport(order = 5)
    public Response<FlowContext> save(@RequestBody FlowParamSave param) {
        return this.flowService.execute(param);
    }

    @Override
    @PostMapping("/start")
    @Operation(summary = "流程-开始")
    @ApiOperationSupport(order = 6)
    public Response<FlowContext> start(@RequestBody FlowParamStart param) {
        return this.flowService.execute(param);
    }

    @Override
    @PostMapping("/action")
    @Operation(summary = "流程-提交&驳回")
    @ApiOperationSupport(order = 7)
    public Response<FlowContext> action(@RequestBody FlowParamAction param) {
        return this.flowService.execute(param);
    }

    @Override
    @PostMapping("/transfer")
    @Operation(summary = "流程-转办")
    @ApiOperationSupport(order = 8)
    public Response<FlowContext> transfer(@RequestBody FlowParamTransfer param) {
        return this.flowService.execute(param);
    }

    @Override
    @PostMapping("/communicate")
    @Operation(summary = "流程-沟通")
    @ApiOperationSupport(order = 9)
    public Response<FlowContext> communicate(@RequestBody FlowParamCommunicate param) {
        return this.flowService.execute(param);
    }


    @Override
    @PostMapping("/communicate/answer")
    @Operation(summary = "流程-回复沟通")
    @ApiOperationSupport(order = 10)
    public Response<FlowContext> communicateAnswer(@RequestBody FlowParamCommunicateAnswer param) {
        return this.flowService.execute(param);
    }

    @Override
    @PostMapping("/cancel")
    @Operation(summary = "流程-取消")
    @ApiOperationSupport(order = 11)
    public Response<FlowContext> cancel(@RequestBody FlowParamCancel param) {
        return this.flowService.execute(param);
    }

    @Override
    @PostMapping("/skip")
    @Operation(summary = "流程-跳过")
    @ApiOperationSupport(order = 12)
    public Response<FlowContext> skip(@RequestBody FlowParamSkip param) {
        return this.flowService.execute(param);
    }
}
